package service

import (
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"tuowei.com/app/api/models"
	"tuowei.com/app/api/service/dto"
	"tuowei.com/common/actions"
	cDto "tuowei.com/common/dto"
)

type SysScore struct {
	service.Service
}

// GetPage 获取Sysscore列表
func (e *SysScore) GetPage(c *dto.SysScoreGetPageReq, p *actions.DataPermission, list *[]models.SysScore, count *int64) error {
	var err error
	var data models.SysScore

	err = e.Orm.Debug().
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).Table(data.TableName()).Order("created_at desc").
		Find(list).Limit(-1).Offset(-1).Count(count).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	return nil
}

// Insert 创建SysAddress对象
func (e *SysScore) Insert(c *dto.SysScoreInsertReq) error {
	var err error
	var data models.SysScore

	c.Generate(&data)

	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}

	return nil
}

func (e *SysScore) GetAll(c *dto.SysScoreGeAllReq, p *actions.DataPermission, list *[]models.SysScore, count *int64) error {
	var err error
	var data models.SysScore

	err = e.Orm.Debug().
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			actions.Permission(data.TableName(), p),
		).Table(data.TableName()).Order("created_at desc").
		Find(list).Limit(-1).Offset(-1).Count(count).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	return nil
}
